package dev.entao.core.util

import dev.entao.base.decodeAndCast
import dev.entao.base.userName
import kotlin.reflect.KProperty

class AnyMap(capacity: Int = 32) : HashMap<String, Any?>(capacity) {


    fun removeProperty(p: KProperty<*>) {
        this.remove(p.userName)
    }

    operator fun get(prop: KProperty<*>): Any? {
        return this.getValue(this, prop)
    }

    operator fun <V> setValue(thisRef: Any?, property: KProperty<*>, value: V) {
        this[property.userName] = value

    }

    @Suppress("UNCHECKED_CAST")
    inline operator fun <reified V> getValue(thisRef: Any?, property: KProperty<*>): V {
        val v = this[property.userName]
        return property.decodeAndCast(v)
    }

}

